package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chencfgs
 * @date 2019-12-05 11:28
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal() {
        List<Setmeal> list;
        try {
            list = setmealService.findAllSetmeal();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, list);
    }


    //    findById
    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") String id){
        Setmeal setmeal = null;
        try {
            String s = setmealService.findById(id);

            setmeal = JSONObject.parseObject(s,Setmeal.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
}
